import { Injectable } from '@angular/core';

import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';

import { Suggestion } from './rxjs-suggestion.model';

@Injectable()
export class RxjsGithubService {
    requestUrl: string = 'https://api.github.com/users?since=';
    constructor(private http: Http) { }
    getSuggestions(): Observable<Suggestion[]> {
        return this.http.get(this.requestUrl + Math.floor(Math.random() * 500))
            .map(this.extractData)
            .catch(this.handleError);
    }
    private extractData(res: Response) {
        let body = res.json();
        return body || {};
    }
    private handleError(error: Response | any) {
        // In a real world app, we might use a remote logging infrastructure
        let errMsg: string;
        if (error instanceof Response) {
            const body = error.json() || '';
            const err = body.error || JSON.stringify(body);
            errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
        } else {
            errMsg = error.message ? error.message : error.toString();
        }
        console.error(errMsg);
        return Observable.throw(errMsg);
    }
}
